package com.ljh.SimpleTradingSystem.mybatis.mapper;

import com.ljh.SimpleTradingSystem.mybatis.entity.Goods;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface GoodsMapper {
    @Select("select count(*) from goods")
    int countAll();

    @Select("select * from goods limit ${index}, ${pageSize}")
    List<Goods> selectByPage(int index, int pageSize);

    @Select("select count(*) from goods where ${column} = #{value}")
    int countAllByColumn(String column, String value);

    @Select("select * from goods where ${column} = #{value} limit ${index}, ${pageSize}")
    List<Goods> selectByColumn(String column, String value, int index, int pageSize);

    @Insert("insert into goods(merchantId, name, type, releaseTime, price," +
            "commentNum, thumbNum, introduction, imagePath)" +
            "values (#{merchantId}, #{name}, #{type}, #{releaseTime}," +
            "#{price}, #{commentNum}, #{thumbNum}, #{introduction}, #{imagePath})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(Goods goods);

    @Select("select * from goods where merchantId = #{merchantId}")
    List<Goods> selectByMerchantId(int merchantId);

    @Select("select * from goods where id = #{id}")
    Goods selectById(int id);

    @Delete("delete from goods where id = #{id}")
    void deleteById(int id);

    @Select("select * from goods order by ${column} ${rule} limit ${index}, ${pageSize}")
    List<Goods> orderByColumn(String column, String rule, int index, int pageSize);

    @Update("update goods set (thumbNum) = #{num} where id = #{id}")
    void updateThumbNumById(int id, int num);

    @Update("update goods set (commentNum) = #{num} where id = #{id}")
    void updateCommentNumById(int id, int num);
}
